/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

// MCCRadioButton.cpp : implementation file

#include "stdafx.h"
#include "CHScanner.h"
#include "MCCRadioButton.h"

#include <uxtheme.h>

//---------------------------------------------------------------------------

IMPLEMENT_DYNAMIC(MCCRadioButton, CButton)
MCCRadioButton::MCCRadioButton() : CButton()
{
	hTheme = NULL;
	changeTextColor = true;
	textColor = RGB(0xFF, 0, 0xFF);
}

MCCRadioButton::~MCCRadioButton()
{
	if(hTheme!=NULL){
		CloseThemeData(hTheme);
		hTheme = NULL;
	}
}

//---------------------------------------------------------------------------

BEGIN_MESSAGE_MAP(MCCRadioButton, CButton)
	ON_WM_PAINT()
	ON_MESSAGE(WM_THEMECHANGED, OnThemeChanged)
END_MESSAGE_MAP()

//---------------------------------------------------------------------------

void MCCRadioButton::OnPaint()
{
	CPaintDC dc(this); // device context for painting

	CRect rect;
	CString title;
	CFont *theFont;
	CFont *oldFont;
	COLORREF oldColor;

	GetWindowText(title);

	// Get the font
	theFont = GetFont();
	oldFont = dc.SelectObject(theFont);

	// Set transparent background
	dc.SetBkMode(TRANSPARENT);

	// set this according to the checked state
	COLORREF color;
	int checkStatus;
	switch(GetCheck()){
		case BST_CHECKED :
			checkStatus = RBS_CHECKEDNORMAL;
			color = RGB(0x00, 0x00, 0x00);
			break;
		case BST_UNCHECKED :
			checkStatus = RBS_UNCHECKEDNORMAL;
			color = RGB(0xFF, 0xFF, 0xFF);
			break;
		default :
			checkStatus = RBS_UNCHECKEDDISABLED;
			color = RGB(0x80, 0x80, 0x80);
			break;
	}

	unsigned int btnStyle;
	btnStyle = GetButtonStyle();

	// Draw the check mark
	GetClientRect(rect);
	if(btnStyle & BS_LEFTTEXT){
		rect.left = rect.right - rect.Height();
	}
	else{
		rect.right = rect.left + rect.Height();
	}

	if(hTheme==NULL)
		if(IsThemeActive())
			hTheme = OpenThemeData(m_hWnd, L"button");

	if(hTheme!=NULL){
		DrawThemeBackground(hTheme, dc.m_hDC, BP_RADIOBUTTON, checkStatus, rect, NULL);
	}
	else{
		dc.FillSolidRect(rect, color);
		dc.DrawEdge(rect, EDGE_SUNKEN, BF_RECT);
	}

	// Save old text color ...
	oldColor = dc.GetTextColor();
	// and change it
	if(changeTextColor)
		dc.SetTextColor(textColor);

	// Draw the text
	GetClientRect(rect);
	if(btnStyle & BS_LEFTTEXT){
		rect.right -= rect.Height() + 4;
		dc.DrawText(title, rect, DT_VCENTER | DT_SINGLELINE | DT_RIGHT);
	}
	else{
		rect.left += rect.Height() + 4;
		dc.DrawText(title, rect, DT_VCENTER | DT_SINGLELINE);
	}

	// Restore original text color
	dc.SetTextColor(oldColor);

	// Select old font
	dc.SelectObject(oldFont);
}

//---------------------------------------------------------------------------

LRESULT MCCRadioButton::OnThemeChanged(WPARAM, LPARAM lCount)
{
	if(hTheme!=NULL)
		CloseThemeData(hTheme);

	if(IsThemeActive()) hTheme = OpenThemeData(m_hWnd, L"button");
	else                hTheme = NULL;

	return 0;
}

//---------------------------------------------------------------------------


